// 每一个存储的模块，命名规则use开头，store结尾
import { logout,apiAdminLogout } from '@/api/customer'
import { getLocal, setLocal, removeLocal } from '@/utils/local'
import { defineStore } from 'pinia'
import { getUserInfo,apiCheckToken } from '@/api/user'
import type { UserInfo } from '@/api/type'
import { isJSON } from '@/utils'
import { ROLE_TYPE } from '@/const'
import { useChatBus } from '@/hook/useChatBus'
const ID_STR = '__ID__'
const NAME_KEY = '__NAME__'
const FRONT_TOKEN = '__FRONT_TOKEN__'
const USER_INFO = '__USER_INFO__'
const AUTHORITY_STATUS = '__AUTHORITY_STATUS__'
const FORMULA_AUTHORITY = '__FORMULA_AUTHORITY__'
const LOGO_URL = '__LOGO_URL__'
const bus = useChatBus(() => {})
export const useUserInfoStore = defineStore({
  id: 'userInfo', // 必须指明唯一的pinia仓库的id
  state: () => {
    return {
      id: getLocal(ID_STR),
      name: getLocal(NAME_KEY),
      token: getLocal(),
      role: false,
      frontToken: getLocal(FRONT_TOKEN),
      userInfo: (isJSON(getLocal(USER_INFO))? JSON.parse(getLocal(USER_INFO) as string) : {} )as UserInfo,
      authorityStatus: Number(getLocal(AUTHORITY_STATUS)), // 0没有查看权限 1有查看权限
      formulaAuthority: Number(getLocal(FORMULA_AUTHORITY)), // 0没有查看权限 1有查看权限
      logoUrl: getLocal(LOGO_URL),
      replyExpireTime: 0, // 回复等待过期时间
    }
  },
  getters: {},
  actions: {
    loginOut() {
      // 处理退出登录的一些逻辑
      return logout().then(() => {
        bus.emit('clearIntervalValue')
        this.token = ''
        this.frontToken = ''
        this.id = ''
        removeLocal()
        removeLocal(FRONT_TOKEN)
        removeLocal(USER_INFO)
        removeLocal(LOGO_URL)
        removeLocal(NAME_KEY)
        removeLocal(ID_STR)
      })
    },
    adminLogout() {
      return apiAdminLogout().then(() => {
        bus.emit('clearIntervalValue')
        this.token = ''
        removeLocal()
        removeLocal(NAME_KEY)
      })
    },
    setToken(token: string) {
      this.token = token
      setLocal(token)
    },
    setFrontToken(token: string) {
      this.frontToken = token
      setLocal(token, FRONT_TOKEN)
    },

    setID(id: string) {
      this.id = id
      setLocal(id, ID_STR)
    },
    setAuthorityStatus(status: number) {
      this.authorityStatus = status
      setLocal(status, AUTHORITY_STATUS)
    },
    setFormulaAuthority(status: number) {
      this.formulaAuthority = status
      setLocal(status, FORMULA_AUTHORITY)
    },
    setLogoUrl(url: string) {
      this.logoUrl = url
      setLocal(url, LOGO_URL)
    },

    setName(name: string) {
      this.name = name
      setLocal(name, NAME_KEY)
    },
    async setUserInfo() {
      const { data:{entity} } = await getUserInfo()
      this.userInfo = entity
      setLocal(JSON.stringify(entity), USER_INFO)
      this.setID(entity.id)
      this.setName(entity.name? entity.name:'')
      this.setAuthorityStatus(entity.authorityStatus)
      this.setFormulaAuthority(entity.formulaAuthority)
      this.setLogoUrl(entity.logoUrl? entity.logoUrl:'')
      // setLocal(userInfo, USER_INFO)
    },
    async checkToken() {
      const {data:{entity}} = await apiCheckToken()
      if(!entity) {
        this.setFrontToken('')
      }
      return entity
    },
    setUserRoleType(roleType: ROLE_TYPE) {
      this.userInfo.roleType = roleType
    },
    setReplyExpireTime(time: number) {
      this.replyExpireTime = time
    }
  }
})
